#pragma once


#include <iostream>
#include <memory>
#include <string>
#include <functional>
#include <thread>
#include <stdint.h>

#include "asio.hpp"
#include "nwbaseconnection.h"
#include "networkutil_common_def.h"


namespace NetworkUtil {


class NetworkUtil_DllExport UdpServer : public BaseConnection
{
public:
    UdpServer() {}
    UdpServer(const ConnectionConfigInfo& stConnCfgInfo);
    ~UdpServer() override;

    void setRecvCallbackFunc(std::function<void(const std::string& strDataBuf, const asio::ip::udp::endpoint& objSenderEndPoint)> pfnRecvCallback);

    void init() override;
    void deinit() override;

    uint32_t read(char* pDataBuf, uint32_t ui32DataBufLen, asio::ip::udp::endpoint& objSenderEndPoint);
    uint32_t read(std::string& strDataBuf, asio::ip::udp::endpoint& objSenderEndPoint);

    uint32_t write(const char* pDataBuf, uint32_t ui32DataBufLen, const asio::ip::udp::endpoint& objSenderEndPoint);
    uint32_t write(const std::string& strDataBuf, const asio::ip::udp::endpoint& objSenderEndPoint);

private:
    void onRecvDataProcess();

private:
    std::shared_ptr<asio::ip::udp::socket> m_sp4ObjSocket = nullptr;
    std::shared_ptr<std::thread> m_sp4thRecv = nullptr;
    std::function<void(const std::string& strDataBuf, const asio::ip::udp::endpoint& objSenderEndPoint)> m_pfnRecvCallback = nullptr;
};


} // namespace NetworkUtil


